package org.ielts.action;

import static com.yifeng.struts2.ResultParameters.*;
import static com.yifeng.struts2.ResultConstants.*;
import static com.yifeng.struts2.ActionUtils.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.LinkedHashMap;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.ielts.domain.base.Lesson;
import org.ielts.domain.base.SignCourse;
import org.ielts.domain.base.Student;
import org.ielts.domain.log.ChangeSignCourseLog;
import org.ielts.domain.log.ChangeSignCourseType;
import org.ielts.domain.log.ExtendSignCourseLog;
import org.ielts.domain.log.OrderLessonLog;
import org.ielts.domain.log.SchoolLog;
import org.ielts.domain.report.LessonWithListenerCount;
import org.ielts.service.FrontService;
import org.ielts.service.LessonService;
import org.ielts.service.OrderLessonLogService;
import org.ielts.service.OrderLessonService;
import org.ielts.service.SchoolLogService;
import org.ielts.service.SignCourseService;
import org.ielts.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;

import com.yifeng.TimePair;
import com.yifeng.modules.annotations.Operation;
import com.yifeng.utils.JsonResultBuilder;

public class LogAction extends BaseAction {

	@Autowired
	SchoolLogService schoolLogService;

	@Autowired
	StudentService studentService;

	@Autowired
	OrderLessonService orderLessonService;

	@Autowired
	LessonService lessonService;

	@Autowired
	SignCourseService signCourseService;

	@Autowired
	FrontService frontService;

	@Operation(result = FILL)
	public String viewOrderLessonLog() {
		String id = form.readString("id");

		OrderLessonLog orderLessonLog = orderLessonService.findOne(id);

		put("log", orderLessonLog);

		return FILL;
	}

	@Operation(result = FILL)
	public String listOrderLessonLog() {
		String signCourseId = form.readString("signCourseId");

		Iterable<OrderLessonLog> orderLessonLogs = orderLessonService.findBySignCourse(signCourseId);

		put("orderLessonLogs", orderLessonLogs);

		double hours = getHours(orderLessonLogs);

		put("hours", String.format("%.1f", hours));

		return FILL;
	}

	private double getHours(Iterable<OrderLessonLog> orderLessonLogs) {
		double time = 0;

		for (OrderLessonLog orderLessonLog : orderLessonLogs) {
			time += (orderLessonLog.getEndTime().getTime() - orderLessonLog.getBeginTime().getTime());
		}

		double hours = time / (1000 * 60 * 60);

		return hours;
	}

	@Operation(result = FILL)
	public String viewLogByTime() {
		String studentId = form.readString("id");

		Iterable<SchoolLog> students = schoolLogService.findByStudent(studentId);
		put("logs", students);

		Student student = studentService.findOne(studentId);
		put("student", student);

		return FILL;
	}

	@Operation(result = FILL)
	public String viewLogByType() {
		String studentId = form.readString("id");

		Iterable<SchoolLog> students = schoolLogService.findByStudent(studentId);

		HashMap<String, List<SchoolLog>> logTypes = new HashMap<String, List<SchoolLog>>();
		for (SchoolLog schoolLog : students) {
			List<SchoolLog> schoolLogs;
			if (logTypes.containsKey(schoolLog.getLogType())) {
				schoolLogs = logTypes.get(schoolLog.getLogType());
			} else {
				schoolLogs = new ArrayList<SchoolLog>();
				logTypes.put(schoolLog.getLogType(), schoolLogs);
			}

			schoolLogs.add(schoolLog);
		}

		put("logTypes", logTypes);

		Student student = studentService.findOne(studentId);
		put("student", student);

		return FILL;
	}

	public LogAction() {
		types.put("CheckLesson", "查课日志");
		types.put("FinishCourse", "结课记录");
		types.put("FreezeCourse", "冻结课程");
		types.put("ResumeCourse", "恢复课程");
		types.put("OrderLesson", "订课记录");
		types.put("SignCourse", "注册课程");
		types.put("AppointTutor", "辅导分配");
		types.put("Consult", "咨询日志");
		types.put("AppointConsultor", "咨询分配");
		types.put("FirstRecord", "首次来访");
		types.put("ChangeSignCourse", "转课/退费");
		types.put("ExtendSignCourse", "延课");
	}

	public LinkedHashMap<String, String> getTypes() {
		return types;
	}

	public void setTypes(LinkedHashMap<String, String> types) {
		this.types = types;
	}

	private LinkedHashMap<String, String> types = new LinkedHashMap<String, String>();
}